package com.demo2do.mobile.web.controller.foundation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.mobile.entity.FoundationRecord;
import com.demo2do.mobile.entity.FoundationStat;
import com.demo2do.mobile.service.FoundationService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/foundations")
public class FoundationRecordController {
	
	private static final Logger logger = LoggerFactory.getLogger(FoundationRecordController.class);
	
	@Autowired
	private FoundationService  foundationService;
	
	/**
	 * 
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/join", method = RequestMethod.GET)
	public ModelAndView join(@RequestParam(name="mobile", required = false) String mobile) {
		
		ModelAndView modelAndView = new ModelAndView("foundation/foundation-join");

		FoundationRecord foundationRecord = foundationService.load(mobile);
		modelAndView.addObject("foundationRecord", foundationRecord);
		
		FoundationStat stat = foundationService.stat();
		modelAndView.addObject("stat", stat);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param mobile
	 * @param name
	 * @param amount
	 * @return
	 */
	@RequestMapping(value = "/join", method = RequestMethod.POST)
	public @ResponseBody String onJoin(@RequestParam("mobile") String mobile, 
										   @RequestParam("name") String name, 
										   @RequestParam("amount") float amount ) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
			
			FoundationRecord foundationRecord = new FoundationRecord(mobile, name, (int)(amount * 10000));
			foundationService.createOrUpdate(foundationRecord);
			
			httpResult.success().message("认领成功");
			
		} catch (Exception e) {
			
			logger.error("#onJoin - Failed to join this foundation", e);
			
			httpResult.fail("500").message("认领失败");
		}
		
		
		return JsonUtils.toJson(httpResult);
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/search", params = "filter", method = RequestMethod.GET)
	public String search() {
		return "foundation/foundation-search";
	}
	
	/**
	 * 
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value = "/{mobile}", method = RequestMethod.GET)
	public ModelAndView detail(@PathVariable("mobile") String mobile) {
		
		ModelAndView modelAndView = new ModelAndView("foundation/foundation-result");
		
		FoundationRecord foundationRecord = foundationService.load(mobile);
		modelAndView.addObject("foundationRecord", foundationRecord);
		
		return modelAndView;
	}
}
